package com.example.mybatisplus.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.mybatisplus.mapper.StuInfoMapper;
import com.example.mybatisplus.model.domain.ConRecord;
import com.example.mybatisplus.model.domain.DutyTime;
import com.example.mybatisplus.model.domain.StuAndFirApoInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.StuInfoService;
import com.example.mybatisplus.model.domain.StuInfo;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author cft
 * @since 2022-06-24
 * @version v1.0
 */
@Controller
@RequestMapping("/api/stuInfo")
public class StuInfoController {

    private final Logger logger = LoggerFactory.getLogger( StuInfoController.class );

    @Autowired
    private StuInfoService stuInfoService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        StuInfo  stuInfo =  stuInfoService.getById(id);
        return JsonResponse.success(stuInfo);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        stuInfoService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateStuInfo(StuInfo  stuInfo) throws Exception {
        stuInfoService.updateById(stuInfo);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建StuInfo
    *
    */
    @RequestMapping("/create")
    @ResponseBody
	public JsonResponse create(@RequestBody StuInfo stuInfo) throws Exception {
        if(stuInfo.checkInfo()){
            return JsonResponse.success(null);
        }
		stuInfoService.save(stuInfo);
		return JsonResponse.success(stuInfo);
	}


	/*
	 * author:cft
	 * function:得到有待处理初访预约的学生信息
	 */
	@RequestMapping("/getWaitedFirstViewStuInfo")
	@ResponseBody
	public JsonResponse getWaitedFirstViewStuInfo(StuInfo stuInfo) {
		StuInfo stuInfo2 = stuInfoService.getById(stuInfo.getStuId());
		return JsonResponse.success(stuInfo2);
	}

	/*
	* author:tqq
	* 根据学生id查询学生信息中的邮箱地址
	* */
    @PostMapping("/getStuEmail")
    @ResponseBody
    public JsonResponse getStuEmail(@RequestBody ConRecord conRecord) {
        System.out.println(conRecord);
        return JsonResponse.success(stuInfoService.getById(conRecord.getConStuId()).getEmail());
    }
    /*
    * author:tqq
    * 根据学生id查询学生信息
    * */
    @PostMapping("/getStuInfo")
    @ResponseBody
    public JsonResponse getUserName(@RequestBody JSONObject json){
        Long str=Long.parseLong(json.getString("conStuId"));
        return JsonResponse.success(stuInfoService.getById(str));
    }

    /*
     * author:jrh
     * function:根据初访信息查询学生信息
     */
    @Autowired
    StuInfoMapper stuInfoMapper;
    @RequestMapping("/getStu")
    @ResponseBody
    public JsonResponse getStu(@RequestBody JSONObject jsonObject){
        Long id=Long.parseLong(jsonObject.getString("fapoId"));
        List<StuAndFirApoInfo> list=stuInfoMapper.getStu(id);
        return JsonResponse.success(list);
    }
}

